
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Data;
import kaizone.haining.chaobohui.json.NewListDO;
import kaizone.haining.chaobohui.json.NewsItem;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.widget.AdView;
import kaizone.haining.chaobohui.widget.CBHItemView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class CBHListFragment extends Fragment implements DataControl.IDataUpdateListener,
        OnItemClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_news_list.htm";

    private FragmentManager mFragmentManager;

    private ArrayList<NewsItem> mData;

    private ListAdapter mAdapter;

    private DataControl mDataControl;

    public CBHListFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
        mData = new ArrayList<NewsItem>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_listad, null);
        AdView adView = (AdView) view.findViewById(R.id.adpanel);
        adView.loadAdSmall();
        adView.rangeWithPanel(getActivity());
        // adView.setAdImageResource(R.drawable.guanggao1);

        ListView listview = (ListView) view.findViewById(R.id.listview);

        // imageView.setImageResource(R.drawable.x_zbg);
        mAdapter = new ListAdapter();
        listview.setAdapter(mAdapter);
        listview.setOnItemClickListener(this);
        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL);
    }

    @Override
    public Object backParse(String string, String requeststring) {
        NewListDO newsDO = NewListDO.parseJson(string);
        return newsDO;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result != null && result instanceof NewListDO) {
            NewListDO newListDO = (NewListDO) result;
            if (newListDO.success) {
                Data data = newListDO.getData();
                if (data != null) {
                    List<NewsItem> items = (List<NewsItem>) data.getItems();
                    mData.removeAll(mData);
                    mData.addAll(items);
                    mAdapter.notifyDataSetChanged();
                }

            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NewsItem news = mData.get(position);
        int newsid = news.getId();
        DetailActivity.come(getActivity(), DetailActivity.FLAG_NEWS, newsid);

    }

    private class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CBHItemView cbhItemView = null;
            if (convertView == null) {
                convertView = (CBHItemView) View.inflate(getActivity(), R.layout.item_home, null);
                if (getActivity() instanceof CBHActivity) {
                    CBHActivity cbhActivity = (CBHActivity) getActivity();
                    ((CBHItemView) convertView).setImageFetcher(cbhActivity.getImageFetcher());
                }
            }
            cbhItemView = (CBHItemView) convertView;
            cbhItemView.setData(mData.get(position));
            //
            return cbhItemView;
        }

    }

}
